using System;
using System.Drawing;
using System.Windows.Forms;

using FreeFlow.Client;

namespace FreeFlow.WinFormsControls
{
  // TODO - support properly
	/// <summary>
	/// A signature control.
	/// </summary>
	public class SignatureControl : TextBox, IFillable, IRefillable
	{
    /// <summary>
    /// Creates a new <see cref="SignatureControl"/> instance.
    /// </summary>
		public SignatureControl()
		{
    }
    #region IFillable Members

    /// <summary>
    /// Renders the signature control with static layout information using the specified field.
    /// </summary>
    /// <param name="field">Field.</param>
    public void Render(FreeFlow.Client.Field field)
    {
      Multiline = true;

      //signature fields are always read-only
      ReadOnly = true;
      BorderStyle = BorderStyle.FixedSingle;
      BackColor = SystemColors.Window;
    }

    /// <summary>
    /// Fills the signature control with values from the specified field.
    /// </summary>
    /// <param name="field">Field.</param>
    public void Fill(FreeFlow.Client.Field field)
    {
      if (field != null)
      {
        Text = field.Value;
        Modified = false;
      }
    }

    /// <summary>
    /// Fills the field from the control.
    /// </summary>
    /// <param name="field">Field.</param>
    public void FillField(Field field)
    {
      if (field != null)
      {
        field.Value = Text;
      }
    }

    #endregion
    #region IRefillable Members

    /// <summary>
    /// Event triggered when a refill should occur
    /// </summary>
    public event System.EventHandler Refill;

    /// <summary>
    /// Triggers a refill.
    /// </summary>
    /// <param name="e">E.</param>
    protected override void OnLeave(EventArgs e)
    {
      base.OnLeave(e);
      if (Modified)
        if (Refill != null)
          Refill(this, e);
    }

    #endregion
  }
}
